<?php
	define("APPID", "wxe8c2ea84bc64454f");
    define("APPSECRET", "441f5c6e16b53d079ac113f3259eed7d");
    function getAccessToken(){
    	$access_token_from_file=file_get_contents('access_token');
    	$access_token_time_from_file=file_get_contents('access_token_time');
    	$now=time();
    	if ($now-$access_token_time_from_file>7200) {
    		$access_token_api="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
			$data=json_decode(http_get($access_token_api));
			$access_token=$data->access_token;
			file_put_contents("access_token",$access_token);
			file_put_contents("access_token_time",time());
			return $access_token;
    	}else{
    		return $access_token_from_file;
    	}
    }
    function getJsApiTicket(){
    	$jsapi_ticket_from_file=file_get_contents('jsapi_ticket');
    	$jsapi_ticket_time_from_file=file_get_contents('jsapi_ticket_time');
    	$now=time();
    	if ($now-$jsapi_ticket_time_from_file>7200) {
    		$jsapi_ticket_api="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".getAccessToken()."&type=jsapi";
			$data=json_decode(http_get($jsapi_ticket_api));
			$jsapi_ticket=$data->ticket;
			file_put_contents("jsapi_ticket",$jsapi_ticket);
			file_put_contents("jsapi_ticket_time",time());
			return $jsapi_ticket;
    	}else{
    		return $jsapi_ticket_from_file;
    	}
    }
    function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}
	function http_post($url,$param,$post_file=false){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
	        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
	            	$is_curlFile = true;
	        } else {
	        	$is_curlFile = false;
	            	if (defined('CURLOPT_SAFE_UPLOAD')) {
	                	curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
	            	}
	        }
		if (is_string($param)) {
	            	$strPOST = $param;
	        }elseif($post_file) {
	            	if($is_curlFile) {
		                foreach ($param as $key => $val) {
		                    	if (substr($val, 0, 1) == '@') {
		                        	$param[$key] = new \CURLFile(realpath(substr($val,1)));
		                    	}
		                }
	            	}
			$strPOST = $param;
		} else {
			$aPOST = array();
			foreach($param as $key=>$val){
				$aPOST[] = $key."=".urlencode($val);
			}
			$strPOST =  join("&", $aPOST);
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($oCurl, CURLOPT_POST,true);
		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}
	function createNonceStr(){
		$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$re="";
		for ($i=0;$i<13;$i++){ 
			$index=rand(0,strlen($str)-1);
			$re.=$str[$index];
		}
		return $re;
	}